#include "situation.h"
#include <cmath>
#include <iostream>

using namespace std;
using namespace stock_trade;

float Situation::GetQuantityPercent() const
{
    float totalBuy = 0;
    for (int i = 0; i < buy5.size(); i++)
    {
        if (abs(buy5[i].price) < MIN_QUANTITY)
            continue;
        totalBuy += buy5[i].quantity;
    }

    float totalSell = 0;
    for (int i = 0; i < sell5.size(); i++)
    {
        if (abs(sell5[i].price) < MIN_QUANTITY)
            continue;
        totalSell += sell5[i].quantity;
    }

    if (abs(totalBuy - totalSell) < MIN_QUANTITY)
        return 1;
    if (totalSell < MIN_QUANTITY)
        return MAX_PERCENT;
    if (totalBuy < MIN_QUANTITY)
        return MIN_PERCENT;
    return totalBuy / totalSell;
}

float Situation::GetPowerPercent() const
{
    float totalBuy = 0;
    float fairPrice = (buy5[buy5.size() - 1].price + sell5[sell5.size() - 1].price) / 2;
    for (int i = 0; i < buy5.size(); i++)
    {
        if (abs(fairPrice - buy5[i].price) < MIN_QUANTITY)
            continue;
        if (abs(buy5[i].price) < MIN_QUANTITY)
            continue;
        //totalBuy += buy5[i].quantity / (fairPrice - buy5[i].price);
        totalBuy += buy5[i].quantity / (log(i + 2) / log(2));
    }

    float totalSell = 0;
    for (int i = 0; i < sell5.size(); i++)
    {
        if (abs(sell5[i].price - fairPrice) < MIN_QUANTITY)
            continue;
        if (abs(sell5[i].price) < MIN_QUANTITY)
            continue;
        //totalSell += sell5[i].quantity / (sell5[i].price - fairPrice);
        totalSell += sell5[i].quantity / (log(i + 2) / log(2));
    }

    //cout << totalBuy << "\t" << totalSell << endl;
    if (abs(totalBuy - totalSell) < MIN_QUANTITY)
        return 1;
    if (totalSell < MIN_QUANTITY)
        return MAX_PERCENT;
    if (totalBuy < MIN_QUANTITY)
        return MIN_PERCENT;
    return totalBuy / totalSell;
}

bool Situation::operator<(const Situation& situation) const
{
    if (this->date < situation.date)
        return true;
    if (this->date > situation.date)
        return false;
    //same date
    if (this->time < situation.time)
        return true;
    if (this->time > situation.time)
        return false;
    return false;
}
